<?php

namespace app\http\middleware;

use app\models\{Agent as model, OperLog, AgentArea, BusinessArea};

use think\Db;

class Agent
{
    public function handle($request, \Closure $next)
    {
        $id = session('agent_id');
        $agent = (new model())->where('id', $id)->find();
        $url = $request->baseUrl();
        if (!$agent && $url != '/index/login') {
            return redirect('index/login');
        }
        $businessid = [];
        $agentlist = [];
        //查询管理区域商家
        if ($agent) {
            $citys = [];
            $areas = [];
            $streets = [];
            $agentarea = (new AgentArea())->where(['agentid' => $agent['id']])->select();
            foreach ($agentarea as $agareas) {
                if ($agareas['type'] == 2) {
                    $citys[] = $agareas['city'];
                }
                if ($agareas['type'] == 3) {
                    $areas[] = $agareas['area'];
                }
                if ($agareas['type'] == 4) {
                    $streets[] = $agareas['street'];
                }
            }
            $whereag1 = [
                'city' => $citys,
                'type' => [3, 4]
            ];
            $whereag2 = [
                'area' => $areas,
                'type' => [4]
            ];
            $where = [
                'city' => $citys,
                'type' => [2, 3, 4]
            ];
            $where1 = [
                'area' => $areas,
                'type' => [3, 4]
            ];
            $where2 = [
                'street' => $streets,
                'type' => [4]
            ];
            $businessid = (new BusinessArea())->whereOr(function ($query) use ($where) {
                $query->where($where);
            })->whereOr(function ($query) use ($where1) {
                $query->where($where1);
            })->whereOr(function ($query) use ($where2) {
                $query->where($where2);
            })->group('businessid')->column('businessid') ?? [];
            $agentlist = (new AgentArea())->whereOr(function ($query) use ($whereag1) {
                $query->where($whereag1);
            })->whereOr(function ($query) use ($whereag2) {
                $query->where($whereag2);
            })->group('agentid')->column('agentid') ?? [];
        }
        $request->agentlist = $agentlist;
        $request->business = $businessid;
        $request->agent = $agent;
        if ($request->isPost() && !empty($agent['id'])) {
            $menu = config('amenu.menu')[strtolower($request->controller())] ?? [];
            $topname = $menu['name'] ?? '';
            $sonname = $menu['sub'][strtolower($request->action())]['name'] ?? '';
            $oper = [
                'type' => 2,
                'adminid' => $agent['id'],
                'topmenu' => $topname,
                'sonmenu' => $sonname,
                'remark' => $request->post()['id'] ?? 0,
                'data' => json_encode($request->post() ?? [], true)
            ];
            if (!empty($oper['topmenu'])) {
                (new OperLog())->allowfield(true)->save($oper);
            }
        }
        return $next($request);
    }
}
